﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using AutoTestEntry.Data;

namespace AutoTestEntry.SpecificUtility
{
    class TestEnvConfig
    {
        private String CONFIG_PATH = GlobalInfo.LocalDestPath + "\\Editor.TestCase\\App.config";
        private XmlDocument doc = new XmlDocument();

        public TestEnvConfig()
        {
           
        }

        private void UpdateKeyValue(String[] keys, String[] values)
        {
           try
           {
               doc.Load(CONFIG_PATH);
               XmlNodeList xns = doc.SelectNodes("/configuration/appSettings/add");

               for (int i = 0; i < keys.Length; ++i)
               {
                   for (int j = 0; j < xns.Count; ++j)
                   {
                       XmlElement xe = (XmlElement)xns[j];
                       if (xe.GetAttribute("key").Equals(keys[i]))
                       {
                           xe.SetAttribute("value", values[i]);
                       }
                   }
               }
               doc.Save(CONFIG_PATH);
               return;
           }
           catch
           {
               return;
           }
        }

        public void SetTestEnvironment(bool useSandbox, String account)
        {
            String[] keys = new String[3] { "UseSandBox", "APIAccountName", "APIAccountDB" };
            String[] values = new String[3];

            if (useSandbox)
            {
                values[0] = "true";
                if (account.Equals("AutoTest2"))
                {
                    values[1] = "AutoTest2";
                    values[2] = "FC_2158120.s3db";
                } 
                else
                {
                    values[1] = "AutoTest3";
                    values[2] = "FC_2158121.s3db";
                }
            }
            else
            {
                values[0] = "false";
                if (account.Equals("AutoTest2"))
                {
                    values[1] = "AutoTest2";
                    values[2] = "FC_2610095.s3db";
                } 
                else
                {
                    values[1] = "AutoTest3";
                    values[2] = "FC_2610939.s3db";
                }
            }
            UpdateKeyValue(keys, values);
        }
    }
}
